import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../../data/profile_data.dart';

class ChangeUserInfoPage extends StatefulWidget {
  const ChangeUserInfoPage({super.key});

  @override
  ChangeUserInfoPageState createState() => ChangeUserInfoPageState();
}

class ChangeUserInfoPageState extends State<ChangeUserInfoPage> {
  final userController = Get.find<UserController>();
  final _formKey = GlobalKey<FormState>();
  final TextEditingController _nameController = TextEditingController();
  String _selectedCampus = ''; // 普通变量存储选择值 [[9]]

  @override
  void initState() {
    super.initState();
    _nameController.text = userController.name.value;
    _selectedCampus = userController.campus.value.isNotEmpty
        ? userController.campus.value
        : '北洋园'; // 初始化默认值 [[9]]
  }

  Future<void> _handleSave() async {
    if (_formKey.currentState!.validate()) {
      _formKey.currentState!.save();

      final success = await userController.updateUserNameCampus(
        name: _nameController.text,
        campus: _selectedCampus,
      );

      if (success) {
        Get.back();
        Get.snackbar(
          "成功",
          "信息修改成功",
          snackPosition: SnackPosition.TOP,
          duration: const Duration(seconds: 2),
        );
      }
    }
  }

  @override
  void dispose() {
    _nameController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("修改个人信息"),
        actions: [
          TextButton(
            onPressed: _handleSave,
            child: const Text(
              "保存",
              style: TextStyle(color: Colors.black, fontSize: 19),
            ),
          ),
        ],
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Form(
          key: _formKey,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              TextFormField(
                controller: _nameController,
                decoration: const InputDecoration(
                  labelText: "昵称",
                  border: OutlineInputBorder(),
                ),
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return "昵称不能为空";
                  }
                  return null;
                },
              ),
              const SizedBox(height: 20),
              DropdownButtonFormField<String>(
                decoration: const InputDecoration(
                  labelText: "校区",
                  border: OutlineInputBorder(),
                ),
                isExpanded: true, // 防止文字溢出 [[6]]
                value: _selectedCampus.isNotEmpty ? _selectedCampus : '北洋园', // 初始值保护 [[9]]
                items: const [ // 使用const优化性能 [[7]]
                  DropdownMenuItem(value: '北洋园', child: Text('北洋园')),
                  DropdownMenuItem(value: '卫津路', child: Text('卫津路')),
                ],
                validator: (value) => value == null ? "请选择校区" : null, // 验证逻辑 [[5]]
                onChanged: (String? newValue) {
                  if (newValue != null) {
                    setState(() {
                      _selectedCampus = newValue; // 状态同步 [[3]]
                    });
                  }
                },
                onSaved: (value) => _selectedCampus = value!, // 表单保存 [[4]]
              ),
            ],
          ),
        ),
      ),
    );
  }
}